package cn.yl.caner.web;

import java.io.IOException;
import java.util.Map;
import java.util.UUID;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.beanutils.BeanUtils;

import cn.yl.caner.domain.RegisteData;
import  cn.yl.caner.service.RegisteService;

public class RegisteServlet extends HttpServlet {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	//注册
	public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
			
		request.setCharacterEncoding("UTF-8");
		//键值对方式进行封装
		Map<String, String[]> properties = request.getParameterMap();
		RegisteData registedata = new RegisteData();
		try {
			BeanUtils.populate(registedata, properties);
		} catch (Exception e) {
			e.printStackTrace();
		}
		//为注册账号设置UUID
		registedata.setId(UUID.randomUUID().toString());
		RegisteService registeservice = new RegisteService();
		int registenumber = registeservice.registeAccount(registedata);
		//状态码判断
		if(registenumber == 0) {
			System.out.println("注册失败");
			response.sendRedirect(request.getContextPath()+"/fail.html");
		}else {
			System.out.println("注册成功");
			response.sendRedirect(request.getContextPath()+"/success.html");
		}
	}

	public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doGet(request, response);
	}
}